package com.ruoyi.mes.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mes.domain.PlnMasterPlan;
import com.ruoyi.mes.domain.SellOrderProductVo;
import com.ruoyi.mes.domain.SleDeliver;
import com.ruoyi.mes.domain.SleOrderPro;
import com.ruoyi.mes.mapper.*;
import com.ruoyi.mes.utils.CodeUtils;
import com.ruoyi.mes.vo.sales.SellOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.domain.SleOrder;
import com.ruoyi.mes.service.ISleOrderService;
import com.ruoyi.mes.mapper.SleOrderProMapper;

/**
 * 销售订单Service业务层处理
 * 
 * @author CFL
 * @date 2024-04-28
 */
@Service
public class SleOrderServiceImpl implements ISleOrderService 
{
    @Autowired
    private SleOrderMapper sleOrderMapper;

    @Autowired
    private PlnMasterPlanMapper plnMasterPlanMapper;

    @Autowired
    private SleDeliverMapper sleDeliverMapper;

    @Autowired
    private SleOrderProductMapper sleOrderProductMapper;

    @Autowired
    private SleOrderProMapper sleOrderProMapper;
    /**
     * 查询销售订单
     * 
     * @param id 销售订单主键
     * @return 销售订单
     */
    @Override
    public SleOrder selectSleOrderById(Long id)
    {
        return sleOrderMapper.selectSleOrderById(id);
    }

    /**
     * 查询销售订单产品信息
     *
     * @param id 销售订单主键
     * @return 销售订单
     */
    @Override
    public List<SellOrderProductVo> selectSleOrderProductById(Long id)
    {
        return sleOrderProductMapper.selectSleOrderProductById(id);
    }

    /**
     * 查询销售订单列表
     * 
     * @param sleOrder 销售订单
     * @return 销售订单
     */
    @Override
    public List<SleOrder> selectSleOrderList(SleOrder sleOrder)
    {
        return sleOrderMapper.selectSleOrderList(sleOrder);
    }

    /**
     * 新增销售订单
     * 
     * @param sleOrder 销售订单
     * @return 结果
     */
    @Override
    public AjaxResult insertSleOrder(SleOrder sleOrder)
    {
        List<SellOrderVo> proInfoList = sleOrder.getProInfoList();

        if (proInfoList.size()==0) {
            return AjaxResult.error("未选择产品！");
        }

        BaseController baseController = new BaseController();
        String username = baseController.getUsername();
        Date nowDate = DateUtils.getNowDate();
        String orderSn = CodeUtils.OrderCodeCreat();
        //插入销售表
        sleOrder.setCreatedBy(username);
        sleOrder.setCreatedTime(nowDate);
        sleOrder.setOrderSn(orderSn);
        sleOrderMapper.insertSleOrder(sleOrder);
        //插入生产计划表
        String planSn = CodeUtils.planCodeCreat();
        PlnMasterPlan plnMasterPlan = new PlnMasterPlan();
        plnMasterPlan.setPlanSn(planSn);
        plnMasterPlan.setPlanState(3);
        plnMasterPlan.setOrderId(sleOrder.getId());
        plnMasterPlan.setCreatedBy(username);
        plnMasterPlan.setCreatedTime(nowDate);
        plnMasterPlanMapper.insertPlnMasterPlan(plnMasterPlan);
        for (int i = 0; i < proInfoList.size(); i++) {
            SellOrderVo sellOrderVo = proInfoList.get(i);
            //插入发货通知表
            SleDeliver sleDeliver = new SleDeliver();
            String deviSn = CodeUtils.deliverCodeCreat();
            sleDeliver.setDeviSn(deviSn);
            sleDeliver.setOrderSn(sleOrder.getOrderSn());
            sleDeliver.setOrderId(sleOrder.getId());
            sleDeliver.setDeviState(0);
            sleDeliver.setProId(sellOrderVo.getProId());
            sleDeliver.setDeviNum(sellOrderVo.getReqNum());
            sleDeliver.setCreatedBy(username);
            sleDeliver.setCreatedTime(nowDate);
            sleDeliverMapper.insertSleDeliver(sleDeliver);



            //插入订单产品联系表
            SleOrderPro sleOrderPro = new SleOrderPro();
            sleOrderPro.setProId(sellOrderVo.getProId());
            sleOrderPro.setProNum(sellOrderVo.getReqNum());
            sleOrderPro.setOrderId(sleOrder.getId());
            sleOrderProMapper.insertSleOrderPro(sleOrderPro);
        }

        return AjaxResult.success("新增成功");
    }

    /**
     * 修改销售订单
     * 
     * @param sleOrder 销售订单
     * @return 结果
     */
    @Override
    public int updateSleOrder(SleOrder sleOrder)
    {
        BaseController baseController = new BaseController();
        String username = baseController.getUsername();
        //设置创建者和创建时间
        sleOrder.setCreatedBy(username);
        sleOrder.setCreatedTime(DateUtils.getNowDate());
        return sleOrderMapper.updateSleOrder(sleOrder);
    }

    /**
     * 批量删除销售订单
     * 
     * @param ids 需要删除的销售订单主键
     * @return 结果
     */
    @Override
    public int deleteSleOrderByIds(Long[] ids)
    {
        return sleOrderMapper.deleteSleOrderByIds(ids);
    }

    /**
     * 删除销售订单信息
     * 
     * @param id 销售订单主键
     * @return 结果
     */
    @Override
    public int deleteSleOrderById(Long id)
    {
        return sleOrderMapper.deleteSleOrderById(id);
    }
}
